package br.com.fiap.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.com.fiap.bean.Favorito;
import br.com.fiap.cfactory.ConnectionFactory;

public class FavoritoDAO {
	
	
Connection conn = null;
	
	public FavoritoDAO(){
		
		try{
			conn = ConnectionFactory.getConnection();
		}
		catch(SQLException ex){ }
	}
	
	public void insert(Favorito f, Integer cId){

		String sql = "INSERT INTO tb_favorito (nome, agencia, conta) VALUES (?,?,?)";
		
		try{
			PreparedStatement stmt = conn.prepareStatement(sql);
			
			stmt.setString(1, f.getNome());
			stmt.setInt(2, f.getAgencia());
			stmt.setInt(3, f.getConta());
			//stmt.setInt(4, cId);
					
			stmt.executeUpdate();

		}
		catch(SQLException ex){ 
			ex.printStackTrace();
		}
		finally{
			
		}
		
	}

	
	public void update(Favorito f){

		String sql = "UPDATE tb_contatos SET nome = ?, agencia = ?, conta = ? WHERE id_favorito = ?";
		
		try{
			PreparedStatement stmt = conn.prepareStatement(sql);
			
			stmt.setString(1, f.getNome());
			stmt.setInt(2, f.getAgencia());
			stmt.setInt(3, f.getConta());
			stmt.setInt(4, f.getId_cliente());
					
			stmt.executeUpdate();

		}
		catch(SQLException ex){ 
			ex.printStackTrace();
		}
		finally{
			
		}
		
	}
	
	public void delete(Integer id){

		String sql = "DELETE FROM tb_favorito WHERE id_favorito = ?";
		
		try{
			PreparedStatement stmt = conn.prepareStatement(sql);
			
			stmt.setInt(1, id);
					
			stmt.executeUpdate();

		}
		catch(SQLException ex){ 
			ex.printStackTrace();
		}
		finally{
			
		}
		
	}
	
	public List<Favorito> getAll(){

		String sql = "SELECT id_favorito, nome, agencia, conta FROM tb_favorito ORDER BY nome";
		
		List<Favorito> favoritos = null;
		
		try{
			PreparedStatement stmt = conn.prepareStatement(sql);
			
			ResultSet rs = stmt.executeQuery();

			favoritos = new ArrayList<Favorito>();
			
			while(rs.next()){
				Favorito f = new Favorito();
				f.setId_cliente((rs.getInt("id_favorito")));
				f.setNome(rs.getString("nome"));
				f.setAgencia(rs.getInt("agencia"));
				f.setConta(rs.getInt("conta"));
				
				favoritos.add(f);
			}
			
			
			
		}
		catch(SQLException ex){ 
			ex.printStackTrace();
		}
		finally{
			
		}
		
		return favoritos;
		
	}
	
	public Favorito getByPK(Integer id){

		String sql = "SELECT id_favorito, nome, agencia, conta FROM tb_favorito WHERE id_favorito = ?";
		
		Favorito f = null;
				
		try{
			PreparedStatement stmt = conn.prepareStatement(sql);
			
			stmt.setInt(1, id);
			
			ResultSet rs = stmt.executeQuery();

			
			if(rs.next()){
				f = new Favorito();
				f.setId_cliente(rs.getInt("id_cliente"));
				f.setNome(rs.getString("nome"));
				f.setAgencia(rs.getInt("agencia"));
				f.setConta(rs.getInt("conta"));
			}
			
		}
		catch(SQLException ex){ 
			ex.printStackTrace();
		}
		finally{
			
		}
		
		return f;
		
	}

	
}
